package at.horario.web.property.editor;

import at.horario.model.Professor;
import at.horario.service.ProfessorManager;
import at.horario.service.TurmaManager;
import at.horario.util.ExcelRow;
import at.horario.util.ByteArrayResourceStream;
import at.horario.web.pages.ProfessorRow;
import org.apache.commons.beanutils.BeanUtils;
import static org.apache.commons.lang.StringUtils.EMPTY;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.apache.wicket.request.target.resource.ResourceStreamRequestTarget;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.util.resource.IResourceStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.util.*;
import java.lang.reflect.InvocationTargetException;

import net.sf.jxls.transformer.XLSTransformer;

public class XlsLink extends Link {
    private static final long serialVersionUID = 1L;
    private IDataProvider dataProvider;
    @SpringBean(name = "professorManager")
    private ProfessorManager professorManager;
    @SpringBean(name = "turmaManager")
    private TurmaManager turmaManager;

    public XlsLink(String id, IDataProvider dataProvider) {
        super(id);
        this.dataProvider = dataProvider;
    }

    public byte[] fill(){
        int size = dataProvider.size();
        Iterator professorRows = dataProvider.iterator(0, size);
        List<ExcelRow> rows = new ArrayList<ExcelRow>(size);
        while (professorRows.hasNext()) {
            ProfessorRow row = (ProfessorRow) professorRows.next();

            ExcelRow r = new ExcelRow();
            Professor professor = professorManager.findBy(row.getHorario(), row.getProfessor());
            r.setCel(professor.toString());

            rows.add(r);

            for (int i = 0; i < row.getHorarios().length; i++) {
                int i1 = row.getHorarios()[i];
                String value;
                if (i1 > 0) {
                    value = turmaManager.findBy(row.getHorario(), i1).toString();
                } else if (i1 == 0) {
                    value = EMPTY;
                } else
                    value = "X";
                try {
                    BeanUtils.setProperty(r, "cel" + (i + 1), value);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                } catch (InvocationTargetException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        }

        Map beans = new HashMap();
        beans.put("rows", rows);
        XLSTransformer transformer = new XLSTransformer();
        //String templateFileName = "c:\\apps\\horario\\src\\main\\resources\\quadroprofessor.xls";
        //String destFileName = "c:\\temp\\teste.xls";
        ClassPathResource templateFileName = new ClassPathResource("quadroprofessor.xls");
        HSSFWorkbook t = null;
        try {
            t = transformer.transformXLS(templateFileName.getInputStream(), beans);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return t.getBytes();

    }

    public void onClick() {
        IResourceStream resourceStream = new ByteArrayResourceStream(fill(), "application/xls");
        getRequestCycle().setRequestTarget(new ResourceStreamRequestTarget(
                resourceStream) {
            public String getFileName() {
                return ("horario.xls");
            }
        });
    }





}